var months = new Array("Janunary", "Feburary", "March", "April", "May", "June", "July",
	 "August", "September", "October", "November", "December");

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// Start with a calendar for today.

function getDays(month, year) 
{
// Test for leap year when February is selected.
  if (1 == month)
    return ((0 == year % 4) && (0 != (year % 100))) ||
      (0 == year % 400) ? 29 : 28;
  else
    return daysInMonth[month];
}

function getToday() {
  // Generate today's date.
  this.now = new Date();
  this.year = this.now.getFullYear();
  this.month = this.now.getMonth();
  this.day = this.now.getDate();
}

function JBList()
{
  this.values= new Array(arguments.length);
  for(i=0;i<arguments.length;i++)
    this.values[i]=arguments[i];
    
  this.selected=0;
  this.set=function (i, newValue)
    {
      if(i<this.value.length)
        this.values[i] = newValue;
    };
    
  this.get=function (i)
    {
      this.selected=i;
      return this.values[i];
    };

  this.getById=function (id)
    {
      for(i=0;i<this.values.length;i++)
      {
        if(this.values[i].id && this.values[i].id == id)
          return this.values[i];
      }
      return null;
    };
    
  this.add=function(newValue)
    {
      this.values[this.values.length]=newValue;
    };
  
  this.next=function()
    {
      this.selected++;
      if(this.selected>=this.values.length)
      {
        this.selected=0;
      }
      return this.values[this.selected];
    };
    
  this.previous=function ()
    {
      this.selected--;
      if(this.selected<0)
      {
        this.selected=this.values.length-1;
      }
      return this.values[this.selected];
    };

  this.last=function ()
    {
      this.selected=this.values.length-1;
      return this.values[this.selected];
    };
    
  this.first=function()
    {
      this.selected=0;
      return this.values[this.selected];
    };

  this.isLast=function ()
    {
      return this.selected >= this.values.length-1;
    };
}


var calendarsOnPage = new JBList();

function newCalendar(eltName, attachedElement) 
{
  this.displayMonth = new Date().getMonth();
  this.displayYear = new Date().getFullYear();
  this.displayDivName = eltName;
  this.today = new getToday();
  this.displayEleName = attachedElement;
  this.id=eltName;
  calendarsOnPage.add(this);
  
  this.makeContent = function () 
  {   
    var parseYear = parseInt(this.displayYear + '');
    var newCal = new Date(parseYear,this.displayMonth,1);
    var day = -1;
    var startDayOfWeek = newCal.getDay();
    if ((this.today.year == newCal.getFullYear()) &&
          (this.today.month == newCal.getMonth()))
    {
       day = this.today.day;
    }
    var intDaysInMonth = getDays(newCal.getMonth(), newCal.getFullYear());
    
    this.daysGrid = makeCalendarView(startDayOfWeek, day, intDaysInMonth, newCal, this.displayDivName)
    if (javaBaseBrowser.dom) {
       var elt = $(this.displayDivName);
       elt.innerHTML = this.daysGrid;
    } else if (javaBaseBrowser.ie) {
       var elt = $(this.displayDivName);
       elt.innerHTML = this.daysGrid;
    } else {
       var elt = document.layers[this.displayDivName].document;
       elt.open();
       elt.write(this.daysGrid);
       elt.close();
    }
  }
}

function incMonth(delta, eltName) {
  ele = calendarsOnPage.getById(eltName);
  
  ele.displayMonth += delta;
  if (ele.displayMonth >= 12) {
   ele.displayMonth = 0;
   incYear(1,eltName);
  } else if (ele.displayMonth <= -1) {
   ele.displayMonth = 11;
   incYear(-1,eltName);
  } else {
   ele.makeContent();
  }
}

function incYear(delta, eltName) {
  ele = calendarsOnPage.getById(eltName);
  ele.displayYear = parseInt(ele.displayYear + '') + delta;
  ele.makeContent();
}

function setDay(day,eltName) {
 ele = calendarsOnPage.getById(eltName);
 $(ele.displayEleName).value = (ele.displayMonth + 1) + "/" + day + "/" + ele.displayYear;
 Element.toggle(eltName);
}
   
function makeCalendarView(startDay, day, intDaysInMonth,newCal,eltName) 
{
  var daysGrid;
  var month = newCal.getMonth();
  var year = newCal.getFullYear();
  var isThisYear = (year == new Date().getFullYear());  
  var isThisMonth = (day > -1)

  daysGrid = '<table ';
  daysGrid += 'class="calendarContainer"';
  daysGrid += 'cellspacing="0" cellpadding="2">';
  daysGrid += '<tr><td colspan="7"  >';
  daysGrid += '<table class="calendarHeader"><tr>';
//  daysGrid += '<td><img src="resources/images/palette/leftArrowActive.gif" onclick="incYear(-5,\'' + eltName + '\')" /></td>';
  daysGrid += '<td class="calendarMonth"><img src="resources/images/windows/Back.gif" onclick="incMonth(-1,\'' + eltName + '\')" /></td>';
  daysGrid += '<td align="center" class="calendarMonth" width="120px">'+months[month]+'</td>';
  daysGrid += '<td class="calendarMonth"><img src="resources/images/windows/Next.gif" onclick="incMonth(1,\'' + eltName + '\')" /></td>';

  daysGrid += '<td class="calendarYear"><img src="resources/images/windows/Back.gif" onclick="incYear(-1,\'' + eltName + '\')" /></td>';
  daysGrid += '<td align="center" class="calendarYear" >'+year+'</td>';
  daysGrid += '<td class="calendarYear"><img src="resources/images/windows/Next.gif" onclick="incYear(1,\'' + eltName + '\')" /></td>';

//  daysGrid += '<td><img src="resources/images/palette/rightArrowActive.gif" onclick="incYear(5,\'' + eltName + '\')" /></td>';
  daysGrid += '<td style="cursor:default;" ><img src="resources/images/windows/Delete.gif" onclick="Element.toggle(\'' + eltName + '\')" /></td>';
  daysGrid += '</tr></table></td></tr>';
  daysGrid += '<tr class="calendarWeek" >';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Su</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Mo</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Tu</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >We</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Th</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Fr</td>';
  daysGrid += '<td style="border-bottom-style:solid; border-width:1px;border-color:#777777;" >Sa</td></tr>';

  var dayOfMonthOfFirstSunday = (7 - startDay + 1);
  weekStr=[];
  for (var intWeek = 0; intWeek < 7; intWeek++) 
  {
    var dayOfMonth;
    weekStr[intWeek] =  '<tr >';
    dayStr=[];
    for (var intDay = 0; intDay < 7; intDay++) 
    { 
      dayOfMonth = (intWeek * 7) + intDay + dayOfMonthOfFirstSunday - 7;
      var cellClass = "calendarNormal";
      if (day > 0 && day == dayOfMonth) cellClass="calendarToday";
             
      dayStr[intDay] = '<td align="center" '
        + ' onclick="setDay('+ dayOfMonth + ',\'' + eltName + '\');" '
        + ' class="'+cellClass+'" ';

      str1 = '';
      if (dayOfMonth <= intDaysInMonth && dayOfMonth>0)
      {
        str1 = ' onMouseOver="this.style.backgroundColor=\'yellow\';"'
          + ' onMouseOut="this.style.backgroundColor=\'\'";';
      }
      
      str1 = str1 + ' >';
      str2 ='';
      if (dayOfMonth <= 0) {
         str2 = "&nbsp;&nbsp; ";
      } else if (dayOfMonth <= intDaysInMonth) {
        var dayString = dayOfMonth ;
        if (dayString.length == 6) dayString = '0' + dayString;
        str2 = dayString;
      }
      str2 = str2 + '</td>';
      dayStr[intDay] = dayStr[intDay] + str1+str2;
    }
    weekStr[intWeek] = weekStr[intWeek] + dayStr.join('')+ "</tr>";
  }
  str = weekStr.join('');
  daysGrid = daysGrid + str;
  return daysGrid;
}

function toggleDatePicker(eltName,formElt) 
{
  ele = calendarsOnPage.getById(eltName);
  if(ele==null)
  {
    calendarHolder=document.createElement('div');
      with(calendarHolder.style)
      {
        display='none';
        position='absolute';  
      }  
    document.body.appendChild(calendarHolder);
    calendarHolder.style.zIndex="1001";
    calendarHolder.id =eltName;
    ele = new newCalendar(eltName, formElt);
    ele.makeContent();
  }
 // JavaBase.DomUtil.changeParent(ele,document.body);
  JB.togglePanel(eltName,{reference:formElt, left:0, top:22});
}

